package com.hjb.springboot.elasticsearch.controller;

import com.hjb.springboot.elasticsearch.entity.City;
import com.hjb.springboot.elasticsearch.repository.CityRepository;
import com.hjb.springboot.elasticsearch.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.*;

@RestController
public class CityController {

    @Autowired
    private CityService cityService;

    @Autowired
    private  ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private CityRepository cityRepository;

    @GetMapping("/city")
    public void save(){

    }

    @GetMapping("/test")
    public void test(){

       /* City city = new City(2L,"广西","是一个美丽的城市",90);
        City city2 = new City(3L,"广西","是一个美丽的城市",90);
        City city3 = new City(4L,"安徽","是一个美丽的城市",90);
        City city4 = new City(5L,"广东","是一个美丽的城市",90);
        City city5 = new City(6L,"广","是一个美丽的城市",90);
        Long id = cityService.save(city);
        List<City> list = new ArrayList<>();
        list.add(city2);
        list.add(city3);
        list.add(city4);
        list.add(city5);
        list.forEach(e->{
            cityService.save(e);
        });*/
       City city = new City();
       city.setDescription("大家好，这是");
       city.setName("好吃");
       city.setId(7L);
       cityService.save(city);
    }
    @GetMapping("/es")
    public List<City> t(String word){
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                //.withQuery(queryStringQuery(word)).build();
                 // .withQuery(matchQuery("name",word)).build();
        .withQuery(multiMatchQuery(word,"name","description")).build();
        List<City> list = elasticsearchTemplate.queryForList(searchQuery,City.class);
        return list;
    }
}
